﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BigLotterySyndicate.Classes;
using SyndicateDAO.DatabaseObject;
using System.Text;

namespace BigLotterySyndicate.MyAccount.Draws
{
    public partial class Default : BasePlayerPage
    {
        public int? DrawId
        {
            get { return Request["dId"] != null ? Int32.Parse(Request["dId"].ToString()) : default(int?); }
        }

        public IQueryable<line> Lines
        {
            get { return this.BusinessFacade.DrawBL.GetLines(CurrentDraw.id); }
        }

        public IQueryable<player> Players
        {
            get { return this.BusinessFacade.DrawBL.GetPlayers(CurrentDraw.id); }
        }

        public draw CurrentDraw
        {
            get
            {
                if (DrawId.HasValue)
                    return this.BusinessFacade.DrawBL.GetSingleById(this.DrawId.Value);
                else
                    return default(draw);
            }
        }

        public bool DrawFinished
        {
            get { return this.CurrentDraw.status == draw.drawstatus.FINISHED; }
        }

        public int ResultBonusBall
        {
            get
            {
                int result = 0;

                if (DrawFinished)
                {
                    string resultsString = this.BusinessFacade.ResultBL.GetResultString(CurrentDraw.id);

                    var a = resultsString.Split(',').ToList();

                    return Int32.Parse(a.Skip(6).FirstOrDefault());
                                        
                }

                return result;
            }
        }

        public List<int> Result
        {
            get
            {
                List<int> result = new List<int>();

                if (DrawFinished)
                {
                    string resultsString = this.BusinessFacade.ResultBL.GetResultString(CurrentDraw.id);

                    var a = resultsString.Split(',').ToList();

                    foreach (string item in a.Take(6))
                    {
                        result.Add(Int32.Parse(item));
                    }
                }

                return result;
            }

        }

        public List<int> ResultEuro
        {
            get
            {
                List<int> result = new List<int>();

                if (DrawFinished)
                {
                    string resultsString = this.BusinessFacade.ResultBL.GetResultString(CurrentDraw.id);

                    var a = resultsString.Split(',').ToList();

                    foreach (string item in a.Take(5))
                    {
                        result.Add(Int32.Parse(item));
                    }
                }

                return result;
            }

        }

        public List<int> ResultEuroLuckyStars
        {
            get
            {
                List<int> result = new List<int>();

                if (DrawFinished)
                {
                    string resultsString = this.BusinessFacade.ResultBL.GetResultString(CurrentDraw.id);

                    var a = resultsString.Split(',').ToList();

                    foreach (string item in a.Skip(5))
                    {
                        result.Add(Int32.Parse(item));
                    }
                }

                return result;
            }

        }

        public List<string> ResultEuroRaffle
        {
            get
            {
                List<string> result = new List<string>();

                if (DrawFinished)
                {
                    string resultsString = this.BusinessFacade.ResultBL.GetResultStringRaffle(CurrentDraw.id);

                    var a = resultsString.Split(',').ToList();

                    foreach (string item in a)
                    {
                        result.Add(item);
                    }
                }

                return result;
            }

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Visible)
                return;

            if (!IsUserLoggedIn)
                return;

            if (CurrentDraw.syndicateId != LoggedInUser.syndicateId)
                Redirect("/MyAccount/Default.aspx?Ns=0");


            this.MasterPagePlayer.SetTopMenuTab(global::BigLotterySyndicate.MyAccount.Controls.TopNav.AdminNavSection.Home);

            this.MasterPagePlayer.BgType = CurrentDraw.type;
            
            this.PlaceHolderLotto.Visible = PlaceHolderLottoResults.Visible = this.CurrentDraw.type == draw.drawType.LOTTO;
            this.PlaceHolderEuro.Visible = PlaceHolderEuroResults.Visible = this.CurrentDraw.type == draw.drawType.EUROMILLIONS;

            SetList();
        }

        private void SetList()
        {
            if (Page.IsPostBack)
                return;

            BindList();
        }

        private void BindList()
        {
            var query = this.Lines;

            if (query.Count() > 0)
            {
                this.RepeaterList.DataSource = query;              
                this.RepeaterList.DataBind();

                this.MultiViewList.SetActiveView(this.ViewList);
            }
            else
            {
                this.MultiViewList.SetActiveView(this.ViewEmpty);
            }
        }

        #region print

        public string PrintResults()
        {
            switch (this.CurrentDraw.type)
            { 
                case draw.drawType.LOTTO:
                    return PrintResultsLotto();
                default:
                    return PrintResultsEuro();
            }
        }
        public string PrintResultsLotto()
        {
            if (this.DrawFinished)
            {
                List<string> numbers = this.BusinessFacade.ResultBL.GetResultString(CurrentDraw.id).Split(',').ToList();

                List<int> a = new List<int>();

                int counter = 1;
                foreach (string item in numbers)
                {
                    if (counter != 7)

                        a.Add(Int32.Parse(item));
                    else
                    {
                        a.Add(Int32.Parse(item) + 100);
                    }
                    counter++;
                }

                a.Sort();


                StringBuilder builder = new StringBuilder();
                builder.Append("<div class=\"numbers\">");
                foreach (int n in a)
                {
                    if (n < 100)
                        builder.AppendFormat("<div class=\"no-match\">{0}</div>", n);
                    else
                        builder.AppendFormat("<div class=\"bonus-match\">{0}</div>", n - 100);
                }
                builder.Append("</div>");

                return builder.ToString();

            }
            else
            {
                return "No results yet, please be patient.";
            }

        }
        public string PrintResultsEuro()
        {
            if (this.DrawFinished)
            {
                List<string> numbers = this.BusinessFacade.ResultBL.GetResultString(CurrentDraw.id).Split(',').ToList();

                var n = numbers.Take(5);               
                               

                StringBuilder builder = new StringBuilder();
                builder.Append("<div class=\"numbers-euro\">");
                foreach (string ball in n)
                {                    
                        builder.AppendFormat("<div class=\"no-match\">{0}</div>", ball);
                
                }
                builder.Append("</div>");

                builder.Append("<div class=\"lucky-stars\">");
                List<string> stars = this.BusinessFacade.ResultBL.GetResultString(CurrentDraw.id).Split(',').ToList();

                var l = stars.Skip(5);

                foreach (string ball in l)
                {
                    builder.AppendFormat("<div class=\"no-match-luckystar\">{0}</div>", ball);
                }
                builder.Append("</div>");
                List<string> raffles = this.BusinessFacade.ResultBL.GetResultStringRaffle(CurrentDraw.id).Split(',').ToList();

                builder.Append("<div class=\"raffle\"  style=\"overflow-x:hidden;height:40px;width:180px;overflow-y:auto\">");
                foreach (string item in raffles)
                {
                    builder.AppendFormat("<div class=\"no-match-raffle\">{0}</div>", item);
                }
                builder.Append("</div>");

                //builder.Append("</div>");

                return builder.ToString();

            }
            else
            {
                return "No results yet, please be patient.";
            }

        }
        
        public string PrintNumbers(Object data)
        {
            switch (this.CurrentDraw.type)
            {
                case draw.drawType.LOTTO:
                    return PrintNumbersLotto(data);
                case draw.drawType.HEALTHLOTTERY:
                    return PrintNumbersHealth(data);
                default:
                    return PrintNumbersEuro(data);
            }
        }
        public string PrintNumbersLotto(Object data)
        {
            line l = data as line;

            List<string> numbers = l.numbers.Split(',').ToList();

            List<int> a = new List<int>();

            foreach (string item in numbers)
            {
                a.Add(Int32.Parse(item));
            }


            a.Sort();

            if (CurrentDraw.status == draw.drawstatus.FINISHED)
            {
                int totalMatch = 0;
                bool bonusMatch = false;

                StringBuilder builder = new StringBuilder();

                builder.Append("<div class=\"numbers\">");
                foreach (int n in a)
                {
                    if (this.Result.Contains(n))
                    {
                        builder.AppendFormat("<div class=\"match\">{0}</div>", n);
                        totalMatch++;
                    }
                    else if(n == this.ResultBonusBall)
                    {
                        builder.AppendFormat("<div class=\"bonus-match\">{0}</div>", n);
                       bonusMatch = true;
                    }
                    else
                    {
                        builder.AppendFormat("<div class=\"no-match\">{0}</div>", n);
                    }

                    

                }

                if (totalMatch >= 5 && bonusMatch)
                    totalMatch++;

                builder.Append("</div>");
                builder.AppendFormat("<div class=\"result\">{0}</div>", totalMatch);

                return builder.ToString();

            }
            else
            {

                StringBuilder builder = new StringBuilder();

                builder.Append("<div class=\"numbers\">");
                foreach (int n in a)
                {
                    builder.AppendFormat("<div class=\"no-match\">{0}</div>", n);
                }
                builder.Append("</div>");
                builder.AppendFormat("<div class=\"result\">&nbsp;</div>");

                return builder.ToString();
            }

        }

        public string PrintNumbersHealth(Object data)
        {
            line l = data as line;

            List<string> numbers = l.numbers.Split(',').ToList();

            List<int> a = new List<int>();

            foreach (string item in numbers)
            {
                a.Add(Int32.Parse(item));
            }


            a.Sort();

            if (CurrentDraw.status == draw.drawstatus.FINISHED)
            {
                int totalMatch = 0;

                StringBuilder builder = new StringBuilder();

                builder.Append("<div class=\"numbers\">");
                foreach (int n in a)
                {
                    if (this.Result.Contains(n))
                    {
                        builder.AppendFormat("<div class=\"match\">{0}</div>", n);
                        totalMatch++;
                    }
                    else
                    {
                        builder.AppendFormat("<div class=\"no-match\">{0}</div>", n);
                    }



                }

                if (totalMatch >= 5)
                    totalMatch++;

                builder.Append("</div>");
                builder.AppendFormat("<div class=\"result\">{0}</div>", totalMatch);

                return builder.ToString();

            }
            else
            {

                StringBuilder builder = new StringBuilder();

                builder.Append("<div class=\"numbers\">");
                foreach (int n in a)
                {
                    builder.AppendFormat("<div class=\"no-match\">{0}</div>", n);
                }
                builder.Append("</div>");
                builder.AppendFormat("<div class=\"result\">&nbsp;</div>");

                return builder.ToString();
            }

        }


        public string PrintNumbersEuro(Object data)
        {
            line l = data as line;

            List<string> numbers = l.numbers.Split(',').Take(5).ToList();

            List<int> a = new List<int>();

            foreach (string item in numbers)
            {
                a.Add(Int32.Parse(item));
            }

            a.Sort();

            List<string> stars = l.numbers.Split(',').Skip(5).ToList();

            List<int> b = new List<int>();

            foreach (string item in stars)
            {
                b.Add(Int32.Parse(item));
            }


            b.Sort();

            List<string> raffle = l.raffle.Split(',').ToList();


            if (CurrentDraw.status == draw.drawstatus.FINISHED)
            {
               
                StringBuilder builder = new StringBuilder();

                builder.Append("<div class=\"numbers-euro\">");
                foreach (int n in a)
                {
                    if (this.ResultEuro.Contains(n))
                    {
                        builder.AppendFormat("<div class=\"match\">{0}</div>", n);
                    }
                    else
                    {
                        builder.AppendFormat("<div class=\"no-match\">{0}</div>", n);
                    }
                }

                builder.Append("</div>");

                builder.Append("<div class=\"lucky-stars\">");


                foreach (int n in b)
                {
                    if (this.ResultEuroLuckyStars.Contains(n))
                    {
                        builder.AppendFormat("<div class=\"match-luckystar\">{0}</div>", n);
                    }
                    else
                    {
                        builder.AppendFormat("<div class=\"no-match-luckystar\">{0}</div>", n);
                    }
                }



                builder.Append("</div>");

                builder.Append("<div class=\"raffle\">");
                foreach (string n in raffle)
                {
                    if (this.ResultEuroRaffle.Contains(n))
                    {
                        builder.AppendFormat("<div class=\"match-raffle\">{0}</div>", n);
                    }
                    else
                    {
                        builder.AppendFormat("<div class=\"no-match-raffle\">{0}</div>", n);
                    }
                }
                builder.Append("</div>");

                return builder.ToString();

            }
            else
            {

                StringBuilder builder = new StringBuilder();

                builder.Append("<div class=\"numbers-euro\">");
                foreach (int n in a)
                {
                    builder.AppendFormat("<div class=\"no-match\">{0}</div>", n);
                }
                builder.Append("</div>");

                builder.Append("<div class=\"lucky-stars\">");
                foreach (int n in b)
                {                   
                        builder.AppendFormat("<div class=\"no-match-luckystar\">{0}</div>", n);
                    
                }
                builder.Append("</div>");

                builder.Append("<div class=\"matches\">");
                foreach (string n in raffle)
                {

                    builder.AppendFormat("<div class=\"no-match-raffle\">{0}</div>", n);

                }
                builder.Append("</div>");

                return builder.ToString();
            }

        }

        public string PrintDrawDate()
        {
            return this.CurrentDraw.type + this.CurrentDraw.drawdate.ToString(", dd MMMM yyyy");
        }

        public string PrintDrawId()
        {
            return this.DrawId.HasValue ? this.DrawId.Value.ToString() : "0";
        }
        #endregion

        
    }
}